<?php
/*//pdo连接信息
$pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root","");
//开始连接数据库
$db = Mysql::newClass();
$db->pdoConnect($pdo);

// 事务开始
  $db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $db->PDO->beginTransaction();

  // $db->query("insert into admin_role(name, creator, description) values('评论模块管理', 2, '评论模块管理')", "");
  // $db->query("insert into admin_role1(name, creator, description) values('评论模块管理2', 2, '评论模块管理')", "");
  // var_dump($db->prepare);
  // $db->PDO->rollBack();
  $db->PDO->commit();
  // 事务结束
  
*/
 
class Mysql
{
    private static $object;
    public $PDO;
    public $prepare;
 
    //单例模式 start
    private function __construct()
    {
    }
 
    public static function newClass()
    {
        if(!(self::$object instanceof self))
        {
            self::$object = new self;
        }
        return self::$object;
    }
    //单例模式 end
 
    //连接pdo
    public function pdoConnect($address)
    {
        try{
            $this->PDO =  new PDO($address[0],$address[1],$address[2]);
            $this->PDO->setAttribute(PDO::ATTR_PERSISTENT,true);
            //设置抛出错误
            $this->PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            //设置当字符串为空时转换为sql的null
            $this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS,true);
            
            // 设置所有返回字段都为string
           $this->PDO->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
            //由MySQL完成变量的转义处理
           $this->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        }catch (PDOException $e)
        {
            return $this->Msg("PDO连接错误信息：".$e->getMessage());
        }
    }
 
    //错误提示
    private function Msg($the_error=""){
        global $databaseInfo;
        $this->PDO = null;
        $this->prepare = null;
        if(!$databaseInfo["debug"]){
          $the_error = "连接数据库出错！";
        }
        $html="<html>
                  <head>
                    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
                    <title>mysql error</title>
                  </head>
                  <body>
                    <div style='width: 50%; height: 200px; border: 1px solid red; font-size: 12px;'>
                       $the_error
                    </div>
                  </body>
               </html>
               ";
        echo $html;
        exit;
    }
    //执行sql，返还影响行数
    public function sqlExec($sql)
    {
         try{
              $result=$this->PDO->exec($sql);
          }catch (PDOException $e)
          {
              $this->Msg($e->getMessage());
          }
         return $result;
    }
    //执行sql语句
    public function query($sql,$where)
    {
       try{
           $this->prepare = $this->PDO->prepare($sql);
       }catch (PDOException $e)
       { 
          $this->Msg("预处理sql出错信息:".$e->getMessage()."<br>sql:(".$sql.")");
       }
       empty($where)? "":$this->bind($where);
       $this->prepare->setFetchMode(\PDO::FETCH_NAMED);
       $this->prepare->execute();
    }
    // 绑定参数
    private function bind($where)
    {
        foreach($where as $key=>$val)
        {
            if(is_string($val)){
                $val = trim($val);
            }
            
            $this->prepare->bindValue(":".$key, $val);
        }
    }
}